package com.admin.socketmultiplayer.activity

import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.SaveListener
import com.admin.socketmultiplayer.R
import com.admin.socketmultiplayer.bean.MyUser
import com.admin.socketmultiplayer.dialog.ChangerServerIp
import com.socks.library.KLog

import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/3/2.
 */

class LoginActivity : BaseActivity() {
    override fun bindLayout(): Int {
        return R.layout.activity_login
    }

    var userName=""
    var password=""
    var serverIp: ChangerServerIp?= null

    override fun initData() {
        tv_found_password.paintFlags= Paint.UNDERLINE_TEXT_FLAG
        tv_found_password.paint.isAntiAlias=true
        tv_register.paintFlags= Paint.UNDERLINE_TEXT_FLAG
        tv_register.paint.isAntiAlias=true

        tv_changer_ip.paintFlags= Paint.UNDERLINE_TEXT_FLAG
        tv_changer_ip.paint.isAntiAlias=true
    }

    override fun initEvent() {
        btn_login.setOnClickListener{
            onViewClicked()
        }

        tv_register.setOnClickListener {
            startActivity<RegisterActivity>()
        }

        tv_found_password.setOnClickListener {
            startActivity<ForgetPasswortActivity>()
        }

        tv_changer_ip.setOnClickListener {
            serverIp?:let {
                serverIp= ChangerServerIp(this,R.layout.dialog_changer_server_ip,R.style.cusdialog)
            }
            serverIp?.let {
                serverIp!!.show()
            }
        }

    }

    private fun onViewClicked() {
        userName=edt_user_name.text.toString()
        if(userName==""){
            toast("用户名能不能为空")
            return
        }
        password=edt_password.text.toString()
        if(password==""){
            toast("密码不能为空")
            return
        }
        var myUser=MyUser()
        myUser.username=userName
        myUser.setPassword(password)
        myUser.login(object :SaveListener<MyUser>(){
            override fun done(p0: MyUser?, p1: BmobException?) {
                p1?:let {
                        startActivity<MainActivity>()
                        finish()
                }

                p1?.let {
                    KLog.d("登陆失败"+p1.message)
                }
            }
        })

    }
}
